package com.cl.blog.web.client;

import com.cl.blog.annotation.Login;
import com.cl.blog.annotation.Permissions;
import com.cl.blog.common.Const;
import com.cl.blog.common.Result;
import com.cl.blog.service.impl.ESService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author 付
 * @Date 2024-05-09 15:10
 * @Version 1.0
 * @description
 * @since 1.0
 */

@RestController
@RequestMapping("/es")
public class SearchController {
	@Autowired
	ESService esService;

	/**
	 * 数据同步
	 *
	 * @return 将数据进行手动同步
	 */
	@GetMapping("/data-syn")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result dataSyn() throws IOException {
		boolean b = esService.dataSynchronization();
		if (b) {
			return Result.success("数据同步成功");
		} else {
			return Result.error("数据同步失败");
		}
	}

	@GetMapping("/search")
	public Result searchDoc(String keyword) throws IOException {
		List<Map<String, Object>> search = esService.search(keyword);
		return Result.success(search);
	}
}
